package com.zhongshengnetwork.rightbe.lang.viewmodel;

import android.content.Intent;
import android.util.Log;

import com.zhongshengnetwork.rightbe.Adv.model.SPManageUtils;
import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.Constant.LetterConstant;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.common.BroadcastDefine;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.GsonTools;
import com.zhongshengnetwork.rightbe.dbmodel.LetterdbModel;
import com.zhongshengnetwork.rightbe.dbmodel.PeopledbModel;
import com.zhongshengnetwork.rightbe.dbservice.LetterdbService;
import com.zhongshengnetwork.rightbe.dbservice.PeopledbService;
import com.zhongshengnetwork.rightbe.gsonmodel.CommonModel;
import com.zhongshengnetwork.rightbe.gsonmodel.PeopleModel;
import com.zhongshengnetwork.rightbe.gsonmodel.SXStateModel;
import com.zhongshengnetwork.rightbe.https.HttpCallBack;
import com.zhongshengnetwork.rightbe.https.HttpsUtils;

import java.util.HashMap;
import java.util.List;

/**
 * Created by luojingxing on 2019/8/5.
 */

public class LetterViewModel {

    //获取私信
    public static void getSX(){
        new Thread(){
            @Override
            public void run() {
                super.run();
                final HashMap<String, String> map = new HashMap<String, String>();
                map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
                String url = "miniapp/letter/"+"langappid"+"/list.do";
                HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        Log.e("TAG","私信列表:"+data);
                        CommonModel commonModel = GsonTools.getCommonModel(data);
                        if (commonModel.getFlag().equals("1")){
                            List<LetterdbModel> list = GsonTools.getLetterdbModel(data);
                            if (list != null && list.size() > 0){
                                Log.e("TAG","私信列表大小:"+list.size());
                                SPManageUtils.getInstance(CustomApplication.mContext).putSPBoolean(LetterConstant.ReceivingKey, true);
                                for (LetterdbModel m : list){
                                    String msgId = CommonUtils.getUUID();
                                    m.setMsgId(msgId);
                                    m.setState(LetterConstant.sx_normal_state);
                                    m.setIsFirst(0);
                                    m.setUnReadCount(0);
                                    m.setIsSelf(0);
                                    m.setTime(System.currentTimeMillis());
                                    if (m.isCancel() == true){
                                        LetterdbModel model = LetterdbService.getById(m.getId());
                                        if (model != null){
                                            m = model;
                                            m.setCancel(true);
                                            m.setMsgId(model.getMsgId());
                                        }
                                        m.setExtra("对方撤回了一条消息");
                                    }
                                    Log.e("TAG","保存私信"+m.getId());
                                    LetterdbService.saveOrUpdate(m);
                                    LetterdbService.updateConversation(m, false);

                                    String target = SPManageUtils.getInstance(CustomApplication.mContext).getSPString(LetterConstant.InChatKey, "");
                                    if (target.equals(m.getFromuserid())){
                                        //通知聊天界面
                                        Intent mIntent = new Intent(LetterConstant.SXChatNotify);
                                        mIntent.putExtra("msgId", msgId);
                                        //发送广播
                                        CustomApplication.mContext.sendBroadcast(mIntent);
                                    }


                                }

                                //通知聊天界面
                                Intent mIntent = new Intent(BroadcastDefine.Update_MsgCount_Action);
                                //发送广播
                                CustomApplication.mContext.sendBroadcast(mIntent);

                            }else {
                                Log.e("TAG","私信列表大小是0");
                                SPManageUtils.getInstance(CustomApplication.mContext).putSPBoolean(LetterConstant.ReceivingKey, false);
                                SPManageUtils.getInstance(CustomApplication.mContext).putSPBoolean(LetterConstant.UnReadOfSXMSGKey, false);
                            }
                        }
                    }

                    @Override
                    public void onError(String msg) {

                    }
                });
            }
        }.start();
    }

    //获取私信状态
    public static void getSXState(){
        new Thread(){
            @Override
            public void run() {
                super.run();
                final HashMap<String, String> map = new HashMap<String, String>();
                map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
                String url = "miniapp/letter/"+"langappid"+"/state.do";
                HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        CommonModel commonModel = GsonTools.getCommonModel(data);
                        if (commonModel.getFlag().equals("1")){
                            SXStateModel m = GsonTools.getSXStateModel(data);
                            if (m != null){
                                SPManageUtils.getInstance(CustomApplication.mContext).putSPBoolean(LetterConstant.SXStateKey, m.isState());
                                SPManageUtils.getInstance(CustomApplication.mContext).putSPInt(LetterConstant.SXTypeKey,m.getType());
                            }
                        }
                    }

                    @Override
                    public void onError(String msg) {

                    }
                });
            }
        }.start();
    }

    //获取黑名单
    public static void getBlack(){
        new Thread(){
            @Override
            public void run() {
                super.run();
                final HashMap<String, String> map = new HashMap<String, String>();
                map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
                map.put("pageRecord","20");
                map.put("pageIndex", SPManageUtils.getInstance(CustomApplication.mContext).getSPInt(LetterConstant.SXBlackPageKey, 0)+"");
                String url = "miniapp/letter/"+"langappid"+"/black/list.do";
                HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        CommonModel commonModel = GsonTools.getCommonModel(data);
                        if (commonModel.getFlag().equals("1")){
                            List<PeopleModel> list = GsonTools.getPeopleModel(data);
                            if (list != null && list.size() > 0){
                                int page = SPManageUtils.getInstance(CustomApplication.mContext).getSPInt(LetterConstant.SXBlackPageKey, 0)+1;
                                SPManageUtils.getInstance(CustomApplication.mContext).putSPInt(LetterConstant.SXBlackPageKey, page);
                                for (PeopleModel m : list){
                                    PeopledbModel model = new PeopledbModel();
                                    model.setUserid(m.getUserid());
                                    model.setFromuserid(m.getRecordUserid());
                                    model.setNickname(m.getNickname());
                                    model.setHeader(m.getHeader());
                                    model.setRemark("");
                                    model.setType(m.getType());
                                    model.setTime((int)(System.currentTimeMillis()/1000));
                                    PeopledbService.saveOrUpdate(model);
                                }
                            }else {
                                SPManageUtils.getInstance(CustomApplication.mContext).putSPBoolean(LetterConstant.SXBlackAsyncKey, true);
                            }
                        }
                    }

                    @Override
                    public void onError(String msg) {

                    }
                });
            }
        }.start();
    }

}
